const fs = require('fs');

// 读取package.json文件
const data = fs.readFileSync('./package.json');

// 项目更新类型，0.0.0 -->  max.mid.min
// 获取到node执行脚本时传入的参数
const type = process.argv.at(-1);

const packageData = JSON.parse(data);
let [max, mid, min] = packageData.version.split('.');
const versionType = { max, mid, min };
versionType[type] = Number(versionType[type]) + 1;
packageData.version = `${versionType.max}.${versionType.mid}.${versionType.min}`;

// 向package.json写入版本号
try {
  fs.writeFileSync('./package.json', JSON.stringify(packageData, null, 2));
} catch (err) {
  console.error('文件写入失败', err);
}
